package com.xgg.taiwuinfo.filter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.xgg.taiwuinfo.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

abstract public class FC_SelectCompareNum extends FilterComponent{
    protected int ReadField;
    protected int TempField;
    protected int ReadCompare;
    protected int TempCompare;
    protected String ReadValue;
    public String TempValue;
    protected List<Map<String, String>> mCompMap;
    protected List<Map<String, String>> mFieldMap;

    protected FC_SelectCompareNum()
    {
        compare_listener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
                TempCompare = (int)id;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}
        };

        field_listener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
                TempField = (int)id;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {}
        };
        text_listener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                TempValue = charSequence.toString();
                if (TempValue.length() == 0)
                {
                    TempValue = "0";
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {}
        };

        if (mCompMap == null) {
            mCompMap = new ArrayList<>();
            AddMap(mCompMap , ">" , FC_GREATER + "");
            AddMap(mCompMap , ">=" , FC_GREATERCONTAIN + "");
            AddMap(mCompMap , "=" , FC_EQUAL + "");
            AddMap(mCompMap , "<=" , FC_SMALLERCONTAIN + "");
            AddMap(mCompMap , "<" , FC_SMALLER + "");
        }

    }

    @Override
    final public int getItemLayout(){ return R.layout.item_fc_selectcompareedit; }

    @Override
    final public void save()
    {
        ReadCompare = TempCompare;
        ReadField = TempField;
        ReadValue = TempValue;
    }

    @Override
    final public JSONObject getJsonObj()
    {
        JSONObject obj = new JSONObject();
        try
        {
            obj.put("id" , mTypeId);
            obj.put("f" , ReadField);
            obj.put("c" , ReadCompare);
            obj.put("v" , ReadValue);
        }
        catch (Exception e)
        {
            Log.d("SaveJson" , e.getLocalizedMessage());
        }
        return obj;
    }

    @Override
    final public void selectCompare(Spinner spinner)
    {
        int itype = TempCompare;
        for (int i = 0; i < mCompMap.size(); i++)
        {
            int type = Integer.parseInt(mCompMap.get(i).get("v"));
            if (itype == type)
            {
                spinner.setSelection(i);
                return;
            }
        }
    }

    @Override
    final public void selectField(Spinner spinner)
    {
        int itype = TempField;
        for (int i = 0; i < mFieldMap.size(); i++)
        {
            int type = Integer.parseInt(mFieldMap.get(i).get("v"));
            if (itype == type)
            {
                spinner.setSelection(i);
                return;
            }
        }
    }

    @Override
    final public List<Map<String, String>> getCompareString(Context ctx){
        return mCompMap;
    }

    @Override
    final public void discard(){
        TempField = ReadField;
        TempCompare = ReadCompare;
        TempValue = ReadValue;
    }

    @Override
    final public boolean isEdited()
    {
        return (!ReadValue.equals(TempValue) || (ReadCompare != TempCompare) || (ReadField != TempField));
    }

    @Override
    final public List<Map<String, String>> getFieldString(Context ctx){
        return mFieldMap;
    }

    @Override
    final public List<Map<String , String>> getValueMap(Context ctx){ return null; }

    @Override
    final public String getDesc()
    {
        for (int i = 0; i < mCompMap.size(); i++)
        {
            Map<String,String> map = mCompMap.get(i);
            if (ReadCompare == Integer.parseInt(map.get("v"))){
                CompStr = map.get("f");
                break;
            }
        }
        for (int i = 0; i < mFieldMap.size(); i++)
        {
            Map<String,String> map = mFieldMap.get(i);
            if (ReadField == Integer.parseInt(map.get("v"))){
                FieldStr = map.get("f");
                break;
            }
        }
        return String.format("%s %s %s\r\n" , FieldStr ,CompStr , ReadValue);
    }
}
